package xyz.xgqn.andsdk;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;

import xyz.xgqn.media.VideoView;

public class VideoViewActivity extends AppCompatActivity {

    VideoView videoView;
    SeekBar seekBar;

    Thread th;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_video_view);
        videoView = findViewById(R.id.video_view);
        seekBar = findViewById(R.id.video_progress);
        seekBar.setMax(100);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                videoView.seek(seekBar.getProgress() / 100.0);
            }
        });

        th = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!th.isInterrupted()) {
                    seekBar.setProgress((int) (100 * videoView.getCurrentPosition()));
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        th.start();
    }

    public void openFile(View view) {
        // /sdcard/test.flv
        videoView.setDataSource("/sdcard/test.flv");
        videoView.start();
    }

    public void openRtmp(View view) {
        // rtsp://192.168.0.10/test.264
        videoView.setDataSource("http://ivi.bupt.edu.cn/hls/cctv5hd.m3u8");
        videoView.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        th.interrupt();
    }
}
